﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh.Admin
{
    public partial class SettingOurServiceDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            LoadListOurService();

            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);
            if (IsValidId)
            {
                LoadData(Id);
            }
            else
            {
                // prepare create new
                txtPriority.Text = "1";
            }
        }

        private void LoadListOurService()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            List<ServiceCategory> listServiceCategory = dbContext.ServiceCategories.OrderBy(obj => obj.Id).ToList();
            ddlServiceCategory.DataSource = listServiceCategory;
            ddlServiceCategory.DataTextField = "Title";
            ddlServiceCategory.DataValueField = "Id";
            ddlServiceCategory.DataBind();
        }

        private void LoadData(int Id)
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();

            OurService ourService = dbContext.OurServices.FirstOrDefault(obj => obj.Id == Id);

            if (ourService == null)
            {
                return;
            }
            ddlServiceCategory.SelectedValue = ourService.ServiceCategoryId.ToString();
            txtPriority.Text = ourService.Priority.ToString();
            txtTitle.Text = ourService.Title;
            txtOverview.Text = ourService.Overview;
            txtContent.Text = ourService.ServiceContent;
            txtImage.Text = ourService.MainImage;
            ddlIsOnHomePage.SelectedValue = ourService.IsOnHomePage.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsValid)
            {
                return;
            }

            if (string.IsNullOrEmpty(Request["Id"]))
            {
                ProcessInsert();
            }
            else
            {
                ProcessUpdate();
            }

            Response.Redirect(@"/Admin/SettingOurService.aspx");
        }

        private void ProcessUpdate()
        {
            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);

            if (!IsValidId)
            {
                return;
            }

            DBTamTreEntities dbContext = new DBTamTreEntities();
            OurService ourService = dbContext.OurServices.FirstOrDefault(obj => obj.Id == Id);

            if (ourService == null)
            {
                return;
            }

            ourService.ServiceCategoryId = Convert.ToInt32(ddlServiceCategory.SelectedValue);
            ourService.Priority = Convert.ToInt32(txtPriority.Text);
            ourService.Title = txtTitle.Text;
            ourService.Overview = txtOverview.Text;
            ourService.ServiceContent = txtContent.Text;
            ourService.MainImage = txtImage.Text;
            ourService.IsOnHomePage = Convert.ToBoolean(ddlIsOnHomePage.SelectedValue);

            dbContext.SaveChanges();
        }

        private void ProcessInsert()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            OurService ourService = new OurService();

            ourService.ServiceCategoryId = Convert.ToInt32(ddlServiceCategory.SelectedValue);
            ourService.Priority = Convert.ToInt32(txtPriority.Text);
            ourService.Title = txtTitle.Text;
            ourService.Overview = txtOverview.Text;
            ourService.ServiceContent = txtContent.Text;
            ourService.MainImage = txtImage.Text;
            ourService.IsOnHomePage = Convert.ToBoolean(ddlIsOnHomePage.SelectedValue);

            dbContext.OurServices.AddObject(ourService);
            dbContext.SaveChanges();
        }
    }
}